package com.prim.threadcoreknowledge.stopthread;

/**
 * run方法阻塞方法每次循环都会sleep或wait等方法 时停止线程
 * 那么不需要每次迭代都检查是否已中断
 */
public class RightWayStopThreadWithEverySleep implements Runnable {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new RightWayStopThreadWithEverySleep());
        thread.start();
        Thread.sleep(5000);
        thread.interrupt();//interrupt只是通知了线程中断，但是线程是否要中断取决于线程本身。
    }

    @Override
    public void run() {
        try {
            int num = 0;
            while (num <= 10000) {
                if (num % 100 == 0) {
                    System.out.println(num + "是100的倍数");
                }
                num++;
                Thread.sleep(10);
            }
            System.out.println("任务执行结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
